package com.ishopping.web.manage.controller;

import com.ishopping.common.core.base.constant.CommonConstants;
import com.ishopping.common.core.base.controller.BaseController;
import com.ishopping.api.manage.entity.Permission;
import com.ishopping.api.manage.enums.ResourceEnum;
import com.ishopping.api.manage.service.IPermissionService;
import com.ishopping.common.core.base.enums.ReturnEnum;
import com.ishopping.common.core.vo.JsonResult;
import com.ishopping.common.core.vo.TreeNode;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限controller
 */
@Controller
@RequestMapping("/system/permission")
public class PermissionController extends BaseController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 权限管理页面
     */
    @RequiresPermissions("system:permission:manage")
    @RequestMapping(value="/manage", method = RequestMethod.GET)
    public String permissionManage(){
        return "system/permissionMgt";
    }

    /**
     * 操作权限集合
     */
    @RequiresPermissions("system:permission:manage")
    @RequestMapping(value="/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Permission> permissionList(Permission permission){
        Long parentId = permission.getParentId();
        List<Permission> permissionList = permissionService.findOperationsByMenuId(parentId);
        return permissionList;
    }

    /**
     * 加载权限树
     * @return List<TreeNode>
     */
    @RequiresPermissions("system:role:assignPermission")
    @RequestMapping(value = "/tree/{roleId}", method = RequestMethod.GET)
    @ResponseBody
    public List<TreeNode> permissionTree(@PathVariable("roleId") Long roleId) {
        return permissionService.findTree(roleId);
    }

    /**
     * 添加权限跳转
     * @param model
     */
    @RequiresPermissions("system:permission:add")
    @RequestMapping(value = "/addForm", method = RequestMethod.GET)
    public String addMenuForm(Model model) {
        model.addAttribute("permission", new Permission());
        model.addAttribute("action", CommonConstants.ADD);
        return "system/permissionForm";
    }

    /**
     * 添加权限
     * @param permission
     */
    @RequiresPermissions("system:permission:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object addMenu(Permission permission) {
        permission.setType(Byte.valueOf(ResourceEnum.OPERATION.getValue()));
        //int result = permissionService.create(permission);
        int result = permissionService.save(permission);
        if(result > 0) {
            return JsonResult.success();
        }else{
            return JsonResult.build(ReturnEnum.USER_FAIL.getValue(), ReturnEnum.USER_FAIL.getText());
        }
    }

    /**
     * 修改权限跳转
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions("system:permission:update")
    @RequestMapping(value = "/updateForm/{id}", method = RequestMethod.GET)
    public String updateMenuForm(@PathVariable("id") Long id, Model model) {
        //model.addAttribute("permission", permissionService.findById(id));
        model.addAttribute("permission", permissionService.queryById(id));
        model.addAttribute("action", CommonConstants.UPDATE);
        return "system/permissionForm";
    }

    /**
     * 修改权限
     * @param permission
     * @return
     */
    @RequiresPermissions("system:permission:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object updateMenu(Permission permission) {
        int result = permissionService.updateSelective(permission);
        if(result > 0) {
            return JsonResult.success();
        }else{
            return JsonResult.build(ReturnEnum.USER_FAIL.getValue(), ReturnEnum.USER_FAIL.getText());
        }
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @RequiresPermissions("system:permission:delete")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object deleteMenu(@PathVariable("id") Long id) {
        int result = permissionService.deleteById(id);
        if(result > 0) {
            return JsonResult.success();
        }else{
            return JsonResult.build(ReturnEnum.USER_FAIL.getValue(), ReturnEnum.USER_FAIL.getText());
        }
    }
}